package co.chuangdian.oflauncher.folders.setting;

import co.chuangdian.oflauncher.OFActivity;
import co.chuangdian.oflauncher.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import cn.onekit.android.ACTIVITY;

@SuppressWarnings("deprecation")
public class HomeActivity extends OFActivity implements
		ActionBar.TabListener {

	//private final static int TAB_INDEX_COUNT = 3;
	private final static int TAB_INDEX_COUNT = 2;
	private ManageFragment eManageFragment = new ManageFragment();
	//private SortFragment eSortFragment = new SortFragment();
	private ViewFragment eViewFragment = new ViewFragment();
	private ViewPager eViewPager;
	private ViewPagerAdapter eViewPagerAdater;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.foldersetting);
		setUpActionBar();
		setUpViewPager();
		setUpTabs();
	}

	private void setUpActionBar() {
		ActionBar action = getActionBar();
		action.setHomeButtonEnabled(false);
		action.setDisplayShowHomeEnabled(false);
		action.setDisplayShowTitleEnabled(false);
		action.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
	}

	private void setUpViewPager() {
		eViewPagerAdater = new ViewPagerAdapter(getSupportFragmentManager());

		eViewPager = (ViewPager) findViewById(R.id.foldersetting_setting_pager);
		eViewPager.setAdapter(eViewPagerAdater);
		eViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				ActionBar action = getActionBar();
				action.setSelectedNavigationItem(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				switch (arg0) {
				case ViewPager.SCROLL_STATE_IDLE:
					// 未拖动页面执行此处
					break;
				case ViewPager.SCROLL_STATE_DRAGGING:
					// 正在拖动页面执行此处
					break;
				case ViewPager.SCROLL_STATE_SETTLING:
					break;

				default:
					break;
				}
			}
		});

	}

	private void setUpTabs() {
		ActionBar action = getActionBar();
		for (int i = 0; i < eViewPagerAdater.getCount(); ++i) {
			action.addTab(action.newTab()
					.setText(eViewPagerAdater.getPageTitle(i))
					.setTabListener(this));
		}

	}

	public class ViewPagerAdapter extends FragmentPagerAdapter {

		public ViewPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			switch (arg0) {
			case 0:
				return eManageFragment;
			case 1:
				return eViewFragment;
				/*
			case 1:
				return eSortFragment;
			case 2:
				return eViewFragment;  */
			}
			throw new IllegalStateException("No fragment at position " + arg0);
		}

		@Override
		public int getCount() {
			return TAB_INDEX_COUNT;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			String tabLabel = null;
			switch (position) {
			case 0:
				tabLabel = getString(R.string.foldersetting_manage);
				break;
			case 1: 
				tabLabel = getString(R.string.foldersetting_view);
				break;
			/*
			case 1:
				tabLabel = getString(R.string.foldersetting_sort);
				break;
			case 2:
				tabLabel = getString(R.string.foldersetting_view);
				break;		*/
			}
			return tabLabel;
		}

	}

	//
	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		eViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {

	}
}
